import Utils from "./utils/Utils";

const {prefixName, assign, defaultTo} = Utils


class UGroup {
  get name(): string {
    return this.options.name
  }
  
  get zIndex(): number {
    return this.options.zIndex
  }
  
  private parts: Part[] = []

  visible: boolean = true

  options: Required<UComponent.LayerOpt> = {
    name: prefixName('u_layer'),
    zIndex: 1
  }
  
  constructor(UGroupOpt?: UComponent.LayerOpt) {
    this.setOption(UGroupOpt)
  }
  
  setOption(UGroupOpt?: UComponent.LayerOpt) {
    this.options = assign(this.options, defaultTo(UGroupOpt, {})) as Required<UComponent.LayerOpt>
  }

  // todo 待实现
  find() {}
  // todo 待实现
  findByName() {}
  // todo 待实现
  findByIndex() {}
  
  
  draw(ctx: CanvasRenderingContext2D) {
    this.parts.sort((a, b) => a.zIndex - b.zIndex)
    this.parts.forEach((layerOrComponent) => layerOrComponent.draw(ctx))
  }

  add(...parts: Part[]) {
    this.parts.push(...parts)
    this.parts.sort((a, b) => a.zIndex - b.zIndex)
  }

  // todo 待实现
  remove(...parts: Part[]) {
    console.log(parts)
  }

  // todo 待实现
  hide(): void {
  }

  // todo 待实现
  show(): void {
  }
}


export default UGroup



